<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="图片名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="图片尺寸" prop="size">
        <el-input
          v-model="queryParams.size"
          class="!w-240px"
          clearable
          placeholder="请输入图片尺寸"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片分类" prop="classify">
        <el-select
          v-model="queryParams.classify"
          class="!w-240px"
          placeholder="请选择图片分类"
          style="width: 240px"
          @keyup.enter="handleQuery"
        >
          <el-option
            v-for="item in classifyList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button plain type="success" @click="batchRevert">
          <Icon class="mr-5px" icon="material-symbols:download" />
          还原
        </el-button>
        <el-button plain type="primary" @click="setClearDays" icon="Edit">
          设置定期清理天数
        </el-button>
        <el-button @click="retreat" type="primary" style="float: right">返回</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 图片列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" label="id" align="center" key="id" prop="id" />
      <el-table-column label="ID" align="center" prop="id" :width="60" />
      <el-table-column prop="url" label="图片" align="center">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.url"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="srcList"
            :initial-index="4"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="资源标题" align="center" />
      <el-table-column prop="batch" label="批次编号" align="center" />
      <el-table-column prop="doi" label="DOI" align="center" />
      <el-table-column prop="source" label="图片来源" align="center" />
      <el-table-column prop="author" label="作者" align="center" />
      <el-table-column prop="size" label="图片尺寸" align="center" />
      <el-table-column prop="format" label="格式" align="center" />
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="updatePicture(scope.row.id)"> 查看 </el-button>
          <el-button link type="success" @click="revert(scope.row.id)"> 还原 </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <el-dialog
    v-model="showImageUploadDialogFlag"
    :title="dialogTitle"
    left
    :append-to-body="true"
    class="drag-dialog small-padding-dialog"
    width="90%"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    align-center
  >
    <div style="margin-top: 10px">
      <div class="block-left">
        <el-form :model="pictureForm" label-width="80px" ref="pictureFormRef">
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="资源标题"
                :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写资源标题' }"
                prop="name"
              >
                <el-input v-model="pictureForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="批次编号" prop="batch">
                <el-input v-model="pictureForm.batch" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="DOI">
                <el-input v-model="pictureForm.doi" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="图片来源">
                <el-input v-model="pictureForm.source" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="发布时间">
                <el-date-picker
                  v-model="pictureForm.releaseTime"
                  type="date"
                  placeholder="请选择发布时间"
                  :disabled-date="disabledDate"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="图片分类">
                <el-select v-model="pictureForm.classify" placeholder="请选择图片分类">
                  <el-option
                    v-for="item in classifyList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="作者">
                <el-input v-model="pictureForm.author" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用途">
                <el-input v-model="pictureForm.useTo" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="图片尺寸">
                <el-input v-model="pictureForm.size" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="格式">
                <el-input v-model="pictureForm.format" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="适用终端">
                <el-input v-model="pictureForm.terminal" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成图方法">
                <el-input v-model="pictureForm.generate" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="质量等级">
                <el-input v-model="pictureForm.quality" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="密级">
                <el-select v-model="pictureForm.secret" placeholder="请选择密级">
                  <el-option
                    v-for="item in secretList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否调修">
                <el-switch v-model="pictureForm.adjustment" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="颜色分类">
                <el-select v-model="pictureForm.color" placeholder="请选择颜色分类">
                  <el-option
                    v-for="item in colorList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="拍摄时间">
                <el-date-picker
                  v-model="pictureForm.captureTime"
                  type="date"
                  placeholder="请选择拍摄时间"
                  :disabled-date="disabledDate"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="相关地点">
                <el-input v-model="pictureForm.place" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="描述">
                <el-input type="textarea" v-model="pictureForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="closeDialog">关闭</el-button>
      </div>
    </template>
  </el-dialog>
  <update ref="updateRef" />

  <!-- 设置定期清理天数对话框 -->
  <ClearDaysForm ref="clearDaysFormRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as pictureApi from '@/api/picture'
import Update from '@/views/picture/update.vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import ClearDaysForm from './ClearDaysForm.vue'
//图片库-回收站列表
defineOptions({ name: 'PictureRecycle' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const queryFormRef = ref() // 搜索的表单
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  type: 10,
  deleted: 1,
  size: null,
  classify: undefined,
  createTime: []
})

const updateRef = ref<any>()

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    let ids = []
    ids.push(id)
    // 发起删除
    await pictureApi.deleteByIds(ids)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const showImageUploadDialogFlag = ref(false)
const dialogTitle = ref('查看图片')
const pictureForm = ref({})
const updatePicture = (id) => {
  getClassify()
  showImageUploadDialogFlag.value = true
  pictureApi.selectDeleted(id, 1).then((res) => {
    pictureForm.value = res
    if (pictureForm.value.classify) {
      pictureForm.value.classify = Number(pictureForm.value.classify)
    }
  })
}

const closeDialog = () => {
  showImageUploadDialogFlag.value = false
}

const classifyList = ref([])
const getClassify = async () => {
  const data = await pictureApi.getPictureClassify()
  classifyList.value = data
}
// 路由信息
const routeInfo = useRoute()

// 设置定期清理天数
const clearDaysFormRef = ref()
const setClearDays = async () => {
  clearDaysFormRef.value.open(routeInfo.meta.title)
}

// 返回按钮
const retreat = () => {
  router.go(-1)
}

const batchRevert = async () => {
  if (selectedItems.value.length == 0 || selectedItems.value.length > 10) {
    ElMessage.warning('请选择要还原的数据!')
    return
  }
  let ids = []
  selectedItems.value.forEach((res) => {
    ids.push(res.id)
  })
  await pictureApi.revert(ids)
  await getList()
}

//还原资源
const revert = async (id) => {
  await message.confirm('确定还原资源吗？')
  let ids = []
  ids.push(id)
  await pictureApi.revert(ids)
  await getList()
}
const selectedItems = ref([])
/** 多选 */
const handleSelectionChange = (val) => {
  selectedItems.value = val
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await pictureApi.recycle(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 初始化 **/
onMounted(async () => {
  await getList()
  getClassify()
})
</script>
